package com.fortune.controller;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.bean.OptionVo;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.CustDAO;
import com.fortune.dal.model.Cust;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("cust")
public class CustController extends AbstractController {

    @Autowired
    private CustDAO custDAO;


    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("cust");

        mv.addObject("menuFlag", "cust");
        return mv;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> list(@ModelAttribute Cust cust) throws BusinessException {

        List<Map<String,Object>> list = Lists.newArrayList();
        try {


            List<Cust> custs = custDAO.queryByModel(cust);

            if(ListUtils.isNotEmpty(custs)){
              for(Cust c:custs){
                  Map<String, Object> map = MapUtils.parseMap(c);
                  String allBirth = c.getBirth();
                  if(allBirth != null && allBirth.length() != 0) {

                      String birth1 = allBirth.substring(0, 4);
                      String birth2 = allBirth.substring(4, 6);
                      String birth3 = allBirth.substring(6, 8);
                      StringBuffer newBirth = new StringBuffer();
                      newBirth.append(birth1);
                      newBirth.append("-");
                      newBirth.append(birth2);
                      newBirth.append("-");
                      newBirth.append(birth3);

                      map.put("newBirth", newBirth);
                  }
                  list.add(map);
              }
            }

        }catch (Exception ex) {
            logger.error("",ex);
            return Lists.newArrayList() ;
        }
        return list;
    }


    @RequestMapping(value = "/select/{queryType}", method = RequestMethod.GET)
    @ResponseBody
    public List<OptionVo> selectOptionVos(@PathVariable String queryType) {
        List<OptionVo> optionVos = Lists.newArrayList();
        try {
            if ("addcust".equals(queryType)) {
                optionVos.add(new OptionVo("男", "男"));
                optionVos.add(new OptionVo("女", "女"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return optionVos;
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp saveProject(@RequestBody Cust cust) {
        BaseResp resp = new BaseResp();
        try {
//            if(cust.getName() == null) {
//                throw new BusinessException("请填写用户姓名") ;
//            }
//            if(StringUtils.isBlank(cust.getSex())) {
//                throw new BusinessException("请选择性别") ;
//            }
//            if(StringUtils.isBlank(cust.getBirth())) {
//                throw new BusinessException("请填写生日日期") ;
//            }
            if(StringUtils.isBlank(cust.getPhone())) {
                throw new BusinessException("请填写电话") ;
            }
//            if(StringUtils.isBlank(cust.getEmail())) {
//                throw new BusinessException("请填写邮箱") ;
//            }
            if(StringUtils.isBlank(cust.getPassword())) {
                throw new BusinessException("请填写密码") ;
            }
            if(cust.getId() == null) {

                Cust param = new Cust();
                param.setName(cust.getName());
//                param.setSex(cust.getSex());
//                param.setBirth(cust.getBirth());
//                param.setPhone(cust.getPhone());
                if(ListUtils.isNotEmpty(custDAO.queryByModel(param))) {
                    throw new BusinessException("用户名重复") ;
                }
                custDAO.insert(cust);
            } else {
                custDAO.updateById(cust) ;
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
